
package com.fire.web.modules.all.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.fire.web.modules.all.constant.ErrorCodeConstants;
import com.fire.web.modules.all.entity.AjaxResult;
import com.fire.web.modules.all.entity.FireMaintainorModel;
import com.fire.web.modules.all.mapper.FireMaintainorMapper;
import com.fire.web.modules.all.mapper.SequenceMapper;
import com.fire.web.modules.all.request.AddMaintainorReq;
import com.fire.web.modules.all.service.IFireMaintainorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 维保单位Service
 *
 * @author Matthew
 * @Date 2019-01-10 10:44
 */
@Service
public class FireMaintainorServiceImpl extends ServiceImpl<FireMaintainorMapper, FireMaintainorModel> implements IFireMaintainorService {

	private static final Logger LOGGER = LoggerFactory.getLogger(FireMaintainorServiceImpl.class);

	@Autowired
	private FireMaintainorMapper fireMaintainorMapper;
	@Autowired
	private SequenceMapper sequenceMapper;

	@Override
	public FireMaintainorModel selectOne(FireMaintainorModel entity) {
		return fireMaintainorMapper.selectOne(entity);
	}

	@Override
	public List<FireMaintainorModel> selectList(FireMaintainorModel model) {
		return fireMaintainorMapper.selectList(model, new EntityWrapper<FireMaintainorModel>());
	}

	@Override
	public List<FireMaintainorModel> selectList(FireMaintainorModel model, Wrapper<FireMaintainorModel> wrapper) {
		return fireMaintainorMapper.selectList(model, wrapper);
	}

	@Override
	public List<FireMaintainorModel> selectPage(Pagination pagination, FireMaintainorModel model, Wrapper<FireMaintainorModel> wrapper) {
		return fireMaintainorMapper.selectPage(pagination, model, wrapper);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public AjaxResult addMaintainor(AddMaintainorReq req, String adminid) {
		FireMaintainorModel fireMaintainorModel = new FireMaintainorModel();
		String id = sequenceMapper.getNextStringValue("maintainor");
		fireMaintainorModel.setMaintainor(id);
		fireMaintainorModel.setCommanyname(req.getCommanyname());
		fireMaintainorModel.setContactphone(req.getContactphone());
		fireMaintainorModel.setCommanyaddress(req.getCommanyaddress());
		fireMaintainorModel.setCommanyname(req.getCommanyname());
		fireMaintainorModel.setCreatetime(new Date());
		fireMaintainorModel.setUpdatetime(fireMaintainorModel.getCreatetime());
		fireMaintainorModel.setEmail(req.getEmail());
		fireMaintainorModel.setOwner(req.getOwner());
		fireMaintainorModel.setPhone(req.getPhone());
		fireMaintainorModel.setWeb(req.getWeb());
		fireMaintainorModel.setSupervisor(req.getSupervisor());
		fireMaintainorModel.setCommanyno(req.getCommanyno());
		fireMaintainorModel.setAdminid(adminid);
		fireMaintainorMapper.insert(fireMaintainorModel);
		return new AjaxResult(ErrorCodeConstants.SUCCESS, "新增成功", "新增成功");
	}

	@Override
	public AjaxResult updateMaintainor(AddMaintainorReq req) {
		FireMaintainorModel fireMaintainorModel = new FireMaintainorModel();
		fireMaintainorModel.setCommanyname(req.getCommanyname());
		fireMaintainorModel.setContactphone(req.getContactphone());
		fireMaintainorModel.setCommanyaddress(req.getCommanyaddress());
		fireMaintainorModel.setCommanyname(req.getCommanyname());
		fireMaintainorModel.setCommanyno(req.getCommanyno());
		fireMaintainorModel.setUpdatetime(new Date());
		fireMaintainorModel.setEmail(req.getEmail());
		fireMaintainorModel.setOwner(req.getOwner());
		fireMaintainorModel.setPhone(req.getPhone());
		fireMaintainorModel.setWeb(req.getWeb());
		fireMaintainorModel.setSupervisor(req.getSupervisor());
		fireMaintainorMapper.update(fireMaintainorModel, new EntityWrapper<FireMaintainorModel>().eq("maintainor", req.getMaintainor()));
		return new AjaxResult(ErrorCodeConstants.SUCCESS, "更新成功", "更新成功");

	}

	@Override
	public AjaxResult getMaintainorSelectItem(){
		List<Map> maintainorSelectItem = fireMaintainorMapper.getMaintainorSelectItem();
		return new AjaxResult(ErrorCodeConstants.SUCCESS, "更新成功", maintainorSelectItem, "更新成功");
	}

}
